import { FETCH_POSTS_SUCCESS, FETCH_POSTS_FAILURE } from './types'
import { call, put } from 'redux-saga/effects'
import { request } from '../../vender/request'

export function* fetchPosts(action: IAction) {
  try {
    const posts = yield call(request.fetchPosts)
    yield put({ type: FETCH_POSTS_SUCCESS, payload: posts.data })
  } catch (err) {
    yield put({
      type: FETCH_POSTS_FAILURE,
      payload: {
        message: '获取 Posts 失败'
      },
      error: true
    })
  }
}
